package com.sgolub.commanndsonline.web;

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.sgolub.commanndsonline.service.ThumbnailGenerator;
import com.sgolub.commanndsonline.service.entity.AdvertisingRegistryService;

@Controller
@RequestMapping("/images")
public class ImagesController {
	
	@Autowired  
    private AdvertisingRegistryService advertisingRegistryService;
	
	@Autowired  
	private ThumbnailGenerator	thumbnailGenerator; 
	String imagesPath = "/home/sgolub/APPPROJECTS/IMAGES";
	//String imagesPath = "D:\\TMP\\IMAGES";
	//String imagesPath = "images";
	   @RequestMapping(value="/small/{imageName}", method = RequestMethod.GET, produces = "image/jpeg")
	   @ResponseBody 
		public  byte[]  getSmall(@PathVariable String imageName,WebRequest request) {
		   byte stub [] =  new byte[]{0};

		   try {
				InputStream inStub;			   
				inStub = new FileInputStream(imagesPath+ "/small/noPhoto.jpg");
				stub =  IOUtils.toByteArray(inStub);
			} catch (FileNotFoundException e1) {
				//e1.printStackTrace();
				System.out.println("Stub File Not Exists");
			} catch (IOException e) {
				//e.printStackTrace();
				System.out.println("Stub File IO Exception");
			}
		   
		   
			try {
				InputStream in;
				in = new FileInputStream(imagesPath+ "/small/" + imageName + ".jpg");
				//System.out.println("File Founded = " + imagesPath+ "/small/" + imageName + ".jpg");
				return IOUtils.toByteArray(in);
			} catch (FileNotFoundException e) {
				//e.printStackTrace();
				System.out.println("File Not Exists");
				//return stub;
			} catch (IOException e) {
				//e.printStackTrace();
				System.out.println("File IO Exceptiom");
				//return stub;
			}
			
			
	    	//File Creation Routines
			String tmpItem = "";
	    	tmpItem = advertisingRegistryService.getItemByInventarNr(imageName).getPhoto();
	    	if (tmpItem == null || tmpItem.length() <1 ) {
	    		System.out.println("File Not Exists In Database");
	    		return stub;
	    	}
	    	
	    	byte[] encoded = Base64.decode(tmpItem.getBytes());
	    
	    	
	    	//System.out.println(encoded);
			try {
				OutputStream out;
				//System.out.println(request.getContextPath() + "/big/" + imageName + ".jpg");
				out = new FileOutputStream(imagesPath+ "/big/" + imageName + ".jpg");
		    	out.write(encoded);
		    	out.close();
		    	System.out.println("File Created = " + imagesPath+ "/big/" + imageName + ".jpg");

				InputStream inTmp;
				inTmp = new FileInputStream(imagesPath+ "/big/" + imageName + ".jpg");		    	
		    	
		    	Image imageIn = ImageIO.read(inTmp);
				System.out.println("Image WIDTH = "  + imageIn.getWidth(null));
				System.out.println("Image HEIGHT = " + imageIn.getHeight(null));		    	

				File outSmall = new File(imagesPath+ "/small/" + imageName + ".jpg");
				thumbnailGenerator.createThumbnail(inTmp, outSmall, 120, imageIn);				

				
				InputStream in;
				in = new FileInputStream(imagesPath+ "/small/" + imageName + ".jpg");
				System.out.println("New Created File Founded = " + imagesPath+ "/small/" + imageName + ".jpg");
				return IOUtils.toByteArray(in);		    	
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				System.out.println("New Created File Not Exists");
			} catch (IOException e) {
				//e.printStackTrace();
				System.out.println("New Created File IO Exceptiom");
			}			
			
		   
			return stub;
		}    
	   
	   
	   @RequestMapping(value="/big/{imageName}", method = RequestMethod.GET, produces = "image/jpeg")
	   @ResponseBody 
		public byte[] getBig(@PathVariable String imageName,WebRequest request) {
		   byte stub [] =  new byte[]{0};

		   try {
				InputStream inStub;			   
				inStub = new FileInputStream(imagesPath+ "/big/noPhoto.jpg");
				stub =  IOUtils.toByteArray(inStub);
			} catch (FileNotFoundException e1) {
				//e1.printStackTrace();
				System.out.println("Stub File Not Exists");
			} catch (IOException e) {
				//e.printStackTrace();
				System.out.println("Stub File IO Exception");
			}
		   
		   
		   
			try {
				InputStream in;
				in = new FileInputStream(imagesPath+ "/big/" + imageName + ".jpg");
				System.out.println("File Founded = " + imagesPath+ "/big/" + imageName + ".jpg");
				return IOUtils.toByteArray(in);
			} catch (FileNotFoundException e) {
				//e.printStackTrace();
				System.out.println("File Not Exists");
				//return stub;
			} catch (IOException e) {
				//e.printStackTrace();
				System.out.println("File IO Exceptiom");
				//return stub;
			}
 
		   
	    	//File Creation Routines
			String tmpItem = "";
	    	tmpItem = advertisingRegistryService.getItemByInventarNr(imageName).getPhoto();
	    	if (tmpItem == null || tmpItem.length() <1 ) {
	    		System.out.println("File Not Exists In Database");
	    		return stub;
	    	}
	    	
	    	byte[] encoded = Base64.decode(tmpItem.getBytes());
	    
	    	
	    	//System.out.println(encoded);
			try {
				OutputStream out;
				//System.out.println(request.getContextPath() + "/big/" + imageName + ".jpg");
				out = new FileOutputStream(imagesPath+ "/big/" + imageName + ".jpg");
		    	out.write(encoded);
		    	out.close();
		    	System.out.println("File Created = " + imagesPath+ "/big/" + imageName + ".jpg");

				InputStream inTmp;
				inTmp = new FileInputStream(imagesPath+ "/big/" + imageName + ".jpg");		    	
		    	
		    	Image imageIn = ImageIO.read(inTmp);
				System.out.println("Image WIDTH = "  + imageIn.getWidth(null));
				System.out.println("Image HEIGHT = " + imageIn.getHeight(null));		    	

				File outSmall = new File(imagesPath+ "/small/" + imageName + ".jpg");
				thumbnailGenerator.createThumbnail(inTmp, outSmall, 120, imageIn);				

				
				InputStream in;
				in = new FileInputStream(imagesPath+ "/big/" + imageName + ".jpg");
				System.out.println("New Created File Founded = " + imagesPath+ "/big/" + imageName + ".jpg");
				return IOUtils.toByteArray(in);		    	
			} catch (FileNotFoundException e) {
				//e.printStackTrace();
				System.out.println("New Created File Not Exists");
			} catch (IOException e) {
				//e.printStackTrace();
				System.out.println("New Created File IO Exceptiom");
			}		

		  return stub;
	}
	   
	   
	
//	    InputStream inputStream = null;  
//	    Resource resource = null;  
//	    try {  
//		    	resource = new ClassPathResource("testFile.txt");  
//		    	inputStream = resource.getInputStream();  
//		    	Scanner scanner0 = new Scanner(inputStream);  
//		    	while (scanner0.hasNext()) {  
//		    		System.out.println(scanner0.nextLine());  
//		    		}  
//	    	} finally {   
//	    		if (inputStream != null) {  
//	    			inputStream.close();  
//	    		}  
//	    	}     
	 
}
